<?php

/**
 * earthQuake actions.
 *
 * @package    TMD
 * @subpackage earthQuake
 * @author     Thaihosting
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class earthQuakeActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex( sfWebRequest $request ) {
		//$currentDate = sfConfig::get( "sf_current_date" );
		
		
		$earthQuake = TMDService::findLastEarthQuake();
		$this->earthQuake = $earthQuake;
		
		$params = array( "limit" => 15, "offset" => 0 );
		if( $request->hasParameter("dateStart") ) {
			
			$startDate = str_replace("/", "-", $request->getParameter("dateStart") );
			$endDate = str_replace("/", "-", $request->getParameter("dateEnd") );
			
			list( $d, $m, $y ) = explode( "-", $startDate );
			
			$params['startDate'] = "$y-$m-$d";
			
			list( $d, $m, $y ) = explode( "-", $endDate );
			$params['endDate'] = "$y-$m-$d";
		}
		
		$earthQuakes = TMDService::findEarthQuakes( $params );
		$this->earthQuakes = $earthQuakes;
		
		$this->dateStart = $request->getParameter("dateStart");
		$this->dateEnd = $request->getParameter("dateEnd");
	}
	
	public function executeEarthquakeContent( sfWebRequest $request ) {
		
		$startDate = str_replace("/", "-", $request->getParameter("startDate") );
		$endDate = str_replace("/", "-", $request->getParameter("endDate") );
		
		$params = array( "statDate"=>$startDate, "endDate"=>$endDate, "offset" =>0 );
		
		/* Load more.. */
		if( $request->getParameter("limit", false) ) {
			$params["limit"] = 100;
			$params["offset"] = 16;
		}
		
		$earthQuakes = TMDService::findEarthQuakes( $params );
		$this->earthQuakes = $earthQuakes;
	}

	public function executeEarthQuakeMapData( sfWebRequest $request ) {
		$culture = $this->getUser()->getCulture();
		$currentDate = sfConfig::get( "sf_current_date" );
		
		$json = '';
		$eks = TMDService::findEarthQuakes( array( "limit" => 3 ) );
		if( $eks && count( $eks ) > 0 ) {
			$idx = 0;
			foreach ( $eks as $ek ) {
// 				$ek = new Earthquake();
				$lat = $ek->getIlatitudeDegree() . "." . $ek->getIlatitudeMinute() . $ek->getIlatitudeSecond();
				$long = $ek->getIlongitudeDegree() . "." . $ek->getIlongitudeMinute() . $ek->getIlongitudeSecond();
				
				$json .= ',{"idx":' . $idx++ . ', "origin": "' . TMDService::renderI18n( $ek, "getSorigin", $culture) . '", "lat":' . $lat . ', "lng": ' . $long .'}';
				
			}
		}
		
		if( strlen( $json ) > 0 ) {
			echo '[' . substr($json, 1, strlen( $json )) . ']';
		}
		else {
			echo "[]";
		}
		
		return sfView::NONE;
	}
}
